<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

switch ($task) {

    case 'quickadd':

        $sql = "SELECT ordering FROM #__leagueman_league ORDER BY ordering DESC LIMIT 1";
        $database->setQuery($sql);
        $ordering = $database->loadResult() + 1;

        $league_name = mosGetParam($_POST, "league_name", "");
        if (!empty($league_name)) {
            $row = LM::getLMTable("League");
            $row->league_name = $league_name;
            $row->ordering = $ordering;
            $row->store();
        }

        LM::redirect("leagues");

        break;

    case 'new':
    case 'edit':

        $row = LM::getLMTable("League");
        $row->load($cid[0]);

        $order = mosGetOrderingList("SELECT ordering AS value, league_name AS text FROM #__leagueman_league ORDER BY ordering");
        $list = mosHTML::selectList($order, 'ordering', 'class="inputbox" size="1"', 'value', 'text', intval($row->ordering));

        $template = LM::getTemplate();
        $template->assign("view", "admin/form_leagues.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("my", $my);
        $template->assign("row", $row);
        $template->assign("list", $list);

        $template->display("admin/view.tpl.php");
        break;

    case 'save':
        $row = LM::getLMTable("League");
        $row->save($_POST);
        LM::redirect("leagues");
        break;

    case 'cancel':
        LM::redirect("leagues");
        break;

    case 'orderup':
        $row = LM::getLMTable("League");
        $row->load($cid[0]);
        $row->move(-1, "");
        LM::redirect("leagues");
        break;

    case 'orderdown':
        $row = LM::getLMTable("League");
        $row->load($cid[0]);
        $row->move(1, "");
        LM::redirect("leagues");
        break;

    case 'remove':
        $row = LM::getLMTable("League");
        foreach ($cid AS $c) {
            $row->delete($c);
        }
        LM::redirect("leagues");
        break;

    default:

        $limit      = LM::getSessionValueFromRequest("_{$act}_limit", 'limit', 10);
        $limitstart = LM::getSessionValueFromRequest("_{$act}_limitstart", 'limitstart', 0);

        $database->setQuery("SELECT COUNT(*) FROM #__leagueman_league AS a");
        $total = $database->loadResult();
        $pageNav = LM::getPageNav($total, $limitstart, $limit, 1);

        $database->setQuery(
            "SELECT a.*
               FROM #__leagueman_league AS a
           ORDER BY a.ordering
              LIMIT $pageNav->limitstart,$pageNav->limit"
        );
        
        $rows = $database->loadObjectList();

        $template = LM::getTemplate();
        $template->assign("view", "admin/list_leagues.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("rows", $rows);
        $template->assign("pageNav", $pageNav);
        $template->assign("my", $my);

        $template->display("admin/view.tpl.php");
        break;
}